package com.tianhui.consignortwofei.other.auth.recognition;

import android.content.Context;

import com.baidu.ocr.sdk.OCR;
import com.baidu.ocr.sdk.OnResultListener;
import com.baidu.ocr.sdk.exception.OCRError;
import com.baidu.ocr.sdk.model.IDCardParams;
import com.baidu.ocr.sdk.model.IDCardResult;
import com.fgs.common.widget.CustomProgressDialog;
import com.tianhui.consignortwofei.mvp.model.enty.RecognitionInfo;
import com.tianhui.consignortwofei.mvp.model.enty.SelectPhotoInfo;
import com.tianhui.consignortwofei.other.auth.inte.RecognitionCallback;
import com.tianhui.consignortwofei.utils.ApplicationUtil;

import java.io.File;

/**
 * @author fgs
 * @date 2021/5/13
 * @description
 **/
public class IdCardBackRecognition implements IRecognition<IDCardResult> {


    @Override
    public void recognition(Context context, SelectPhotoInfo selectPhotoInfo, RecognitionCallback<IDCardResult> callback) {
        IDCardParams param = new IDCardParams();
        param.setImageFile(new File(selectPhotoInfo.filePath));
        // 设置身份证正反面
        param.setIdCardSide(IDCardParams.ID_CARD_SIDE_BACK);
        // 设置方向检测
        param.setDetectDirection(true);
        // 设置图像参数压缩质量0-100, 越大图像质量越好但是请求时间越长。 不设置则默认值为20
        param.setImageQuality(20);

        CustomProgressDialog customProgressDialog = new CustomProgressDialog(context);
        customProgressDialog.show();
        OCR.getInstance(ApplicationUtil.getContext()).recognizeIDCard(param, new OnResultListener<IDCardResult>() {
            @Override
            public void onResult(IDCardResult result) {
                customProgressDialog.dismiss();
                if (result != null) {
                    RecognitionInfo recognitionInfo = new RecognitionInfo<IDCardResult>();
                    recognitionInfo.info = result;
                    recognitionInfo.filePath = selectPhotoInfo.filePath;
                    if (null != callback)
                        callback.recognitionSuccess(recognitionInfo);
                }
            }

            @Override
            public void onError(OCRError error) {
                customProgressDialog.dismiss();
                if (null != callback)
                    callback.recognitionFailed();
            }
        });
    }
}
